import base from '../base/base'

export default function decMap (params) {
  this.target = params.target
  this.AMap = params.AMap
  this.mapStyle = params.mapStyle || ''
  this.map = {}
  // 初始宽高
  this.w = params.w
  this.h = params.h
  this.base = base()
  this.data = params.data
}
decMap.prototype = {
  constructor: decMap,
  render: function () {
    let $target = $(this.target)
    let AMap = this.AMap
    let data = this.data.result.nodes
    let _this = this
    $target.css({
      'width': this.w,
      'height': this.h
    })
    this.map = new AMap.Map('deviceMap', {
      mapStyle: this.mapStyle
    })
    if (!data.length) {
      let point = new AMap.LngLat(121.423224, 31.223914)
      _this.map.setCenter(point)
      _this.map.setZoom(19)
    }
    // console.log(data)
    this.addMarker(0, data)
  },
  createPopHtml: function (data) {
    let sContent = ''
    sContent += '<div class="mapStyle">'
    sContent += '<h2>详情</h2>'
    sContent += '<ul>'
    sContent += '<li class="clearfix"><div class="mapStyle-left">类型:</div><div class="mapStyle-right">' + data.equipmentType + '</div></li>'
    sContent += '<li class="clearfix"><div class="mapStyle-left">名称:</div><div class="mapStyle-right">' + data.equipmentName + '</div></li>'
    sContent += '<li class="clearfix"><div class="mapStyle-left">地址:</div><div class="mapStyle-right">' + data.equipmentAddr + '</div></li>'
    sContent += '<li class="clearfix"><div class="mapStyle-left">状态:</div><div class="mapStyle-right">' + data.equipmentNow + '</div></li>'
    sContent += '</ul>'
    sContent += '</div>'
    return sContent
  },
  showName: function (obj) {
    let nodes = this.data.result.nodes
    let tempArray = []
    this.map.clearMap()
    for (let i = 0; i < obj.length; i++) {
      if (obj[i].deviceState) {
        tempArray.push(nodes[i])
      }
    }
    // console.log(obj);
    // console.log(tempArray);
    if (tempArray.length) {
      this.addMarker(1, tempArray)
    }
  },
  addMarker: function (initial, data) {
    let AMap = this.AMap
    for (let i = 0; i < data.length; i++) {
      const item = data[i]
      let _src = item.equipmentSrc
      let _nodes = item.nodes
      for (let j = 0; j < _nodes.length; j++) {
        let point = new AMap.LngLat(_nodes[j].gpsLat, _nodes[j].gpsLng)
        let marker = new AMap.Marker({
          position: point,
          icon: new AMap.Icon({
            size: new AMap.Size(25, 25),
            image: _src
          }),
          offset: new AMap.Pixel(-19, -60),
          resizeEnable: true,
          title: _nodes[j].equipmentMaker
        })
        // let infoWindow = new AMap.InfoWindow({
        //   content: this.createPopHtml(_nodes[j])
        // })
        if (initial === 0) {
          this.map.setCenter(point)
        }
        this.map.add(marker)
        this.map.setZoom(19)
        // (function (iw, it) {
        //   marker.on("click", function (e) {
        //     iw.open(_this.map, e.lnglat);
        //   });
        // })(infoWindow, _nodes[j]);
      }
    }
  },
  init: function () {
    this.render()
  }
}
